package com.key.common.service.serviceImp;

import java.io.Serializable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.key.common.model.Page;

/**
 * @project paging
 * @path wilr.utils.paging.taglib
 * @desc 分页标签
 * @author Wilr
 * @QQ:545881075
 * @time 2012-2-28 上午12:02:21
 */
public class PagingTag extends TagSupport implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 是否显示改变页数下拉框
	 */
	private boolean skipCount = true;
	/**
	 * 是否显示跳页输入框
	 */
	private boolean jumpText = true;
	/**
	 * 是否显示上/下一页按钮
	 */
	private boolean prevPage = true;
	/**
	 * 是否显示首/尾页按钮
	 */
	private boolean startPage = true;
	/**
	 * 是否显示遍历页
	 */
	private boolean forPage = true;
	/**
	 * 是否显示当前页和总页数
	 */
	private boolean showCurAndSum = true;
	/**
	 * 是否启用刷新
	 */
	private boolean refresh = true;
	/**
	 * 分页实体对象
	 */
	private Page page;
	/**
	 * 请求URL
	 */
	private String url;

	public boolean isSkipCount() {
		return skipCount;
	}

	public void setSkipCount(boolean skipCount) {
		this.skipCount = skipCount;
	}

	public boolean isJumpText() {
		return jumpText;
	}

	public void setJumpText(boolean jumpText) {
		this.jumpText = jumpText;
	}

	public boolean isPrevPage() {
		return prevPage;
	}

	public void setPrevPage(boolean prevPage) {
		this.prevPage = prevPage;
	}

	public boolean isStartPage() {
		return startPage;
	}

	public void setStartPage(boolean startPage) {
		this.startPage = startPage;
	}

	public boolean isForPage() {
		return forPage;
	}

	public void setForPage(boolean forPage) {
		this.forPage = forPage;
	}

	public boolean isShowCurAndSum() {
		return showCurAndSum;
	}

	public void setShowCurAndSum(boolean showCurAndSum) {
		this.showCurAndSum = showCurAndSum;
	}

	public boolean isRefresh() {
		return refresh;
	}

	public void setRefresh(boolean refresh) {
		this.refresh = refresh;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 输出分页标签
	 */
	public int doStartTag() throws JspException {
		try {
			if (page != null) {
				long currentPage = page.getCurrentPage(); // 获得当前页面
				long totalPage = page.getTotalPage(); // 获得总页数
				StringBuilder builder = new StringBuilder(
						"<script type='text/javascript'>");
				if (url.indexOf("?") > 0) {
					builder
							.append("function loadPage(pagenum){var url='"
									+ url
									+ "&page.currentPage='+pagenum;self.location.href=url}");
				} else {
					builder
							.append("function loadPage(pagenum){var url='"
									+ url
									+ "?page.currentPage='+pagenum;self.location.href=url}");
				}
				builder
						.append("function jumpPage(){var jumpPageNum=document.getElementById('jump_page_num').value;");
				builder
						.append("if(jumpPageNum.length<=0){alert('请填写要跳转的页面值!');return false;}");
				builder
						.append("if(!(/^[1-9]\\d*$/).test(jumpPageNum)){alert('跳转页面值只能填写正整数');return false;}");
				builder.append("if(jumpPageNum>" + page.getTotalPage()
						+ "){alert('跳转的页数大于总页数!');return false;}");
				if (url.indexOf("?") > 0) {
					builder
							.append("var url='"
									+ url
									+ "&page.currentPage='+jumpPageNum;self.location.href=url }");
				} else {
					builder
							.append("var url='"
									+ url
									+ "?page.currentPage='+jumpPageNum;self.location.href=url }");
				}
				builder
						.append("function refreshPage(){self.location.href=self.location.href;}");
				builder.append("</script>");
				builder.append("<div class='page'><ul>");
				// 如果当前页面为第一页
				if (currentPage == 1) {
					if (startPage)
						builder
								.append("<li class='start_off'><span>首页</span></li>");
					if (prevPage)
						builder
								.append("<li class='first_off'><span>前一页</span></li>");
				}
				// 当前页面不是第一页
				else {
					if (startPage)
						builder
								.append("<li class='start' onclick='javascript:loadPage(\"1\");'><span>首页</span></li>");
					if (prevPage)
						builder
								.append("<li class='first' onclick='javascript:loadPage(\""
										+ (page.getCurrentPage() - 1)
										+ "\");'><span>前一页</span></li>");
				}
				// 判断是否显示google风格的分页
				if (forPage && totalPage > 0) {
					builder.append("<li>");
					builder.append("<div class='pagenumber'>");
					builder.append("<ul>");
					builder.append("<li class='fleft'></li>");
					/**
					 * 每次只取9页
					 */
					for (long pagenum = 1; pagenum <= page.getTotalPage(); pagenum++) {
						if (page.getCurrentPage() < 5 && pagenum < 10) {
							if (pagenum == page.getCurrentPage())
								builder.append("<li class='nowPage' title='第"
										+ pagenum + "页'>" + pagenum + "</li>");
							else {
								if (url.indexOf("?") > 0) {
									builder
											.append("<li class='fcenter' title='第"
													+ pagenum
													+ "页'><a href='"
													+ url
													+ "&page.currentPage="
													+ pagenum
													+ "'>"
													+ pagenum
													+ "</a></li>");
								} else {
									builder
											.append("<li class='fcenter' title='第"
													+ pagenum
													+ "页'><a href='"
													+ url
													+ "?page.currentPage="
													+ pagenum
													+ "'>"
													+ pagenum
													+ "</a></li>");
								}
							}
						} else {
							// 页面大于5以后,当前页位于中间
							if (pagenum > page.getCurrentPage() - 5
									&& pagenum < page.getCurrentPage() + 5) {
								if (pagenum == page.getCurrentPage())
									builder
											.append("<li class='nowPage' title='第"
													+ pagenum
													+ "页'>"
													+ pagenum
													+ "</li>");
								else {
									if (url.indexOf("?") > 0) {
										builder
												.append("<li class='fcenter' title='第"
														+ pagenum
														+ "页'><a href='"
														+ url
														+ "&page.currentPage="
														+ pagenum
														+ "'>"
														+ pagenum + "</a></li>");
									} else {
										builder
												.append("<li class='fcenter' title='第"
														+ pagenum
														+ "页'><a href='"
														+ url
														+ "?page.currentPage="
														+ pagenum
														+ "'>"
														+ pagenum + "</a></li>");
									}
								}
							}
						}
					}
					builder.append("<li class='fright'></li>");
					builder.append("</ul>");
					builder.append("</div>");
					builder.append("</li>");
				}

				// 如果当前页面为尾页
				if (currentPage == totalPage || totalPage <= 0) {
					if (prevPage)
						builder
								.append("<li class='next_off'><span>后一页</span></li>");
					if (startPage)
						builder
								.append("<li class='end_off'><span>尾页</span></li>");

				}
				// 当前页面不是尾页
				else {
					if (prevPage)
						builder
								.append("<li class='next' onclick='javascript:loadPage(\""
										+ (page.getCurrentPage() + 1)
										+ "\");'><span>后一页</span></li>");
					if (startPage)
						builder
								.append("<li class='end' onclick='javascript:loadPage(\""
										+ page.getTotalPage()
										+ "\");'><span>尾页</span></li>");
				}
				/*
				 * 先屏蔽 //如果启用页面显示页数选择 这里启用5 10 15 20供用户选择
				 */
				// if (skipCount) {
				// builder.append("<li>");
				// builder
				// .append("<select class='rounded' name='page.pageSize'");
				// builder.append("<option value='5'>5条/页</option>");
				// builder
				// .append("<option value='10'
				// selected='selected'>10条/页</option>");
				// builder.append("<option value='15'>15条/页</option>");
				// builder.append("<option value='20'>20条/页</option>");
				// builder.append("</select>");
				// builder.append("</li>");
				// }
				// 如果启用页面跳转
				if (jumpText && totalPage > 0) {
					builder.append("<li>");
					builder
							.append("<input type='text' size='5' name='page.currentPage' id='jump_page_num' class='rounded jumpInput'>");
					builder.append("</li>");
					builder
							.append("<li class='jump' onclick='javascript:jumpPage();'><span>跳转</span></li>");
				}
				// 如启用总页数显示
				if (showCurAndSum) {
					builder.append("<li class='pageInfo'>共<span>"
							+ page.getTotalCount() + "</span>条/共<span>"
							+ page.getTotalPage() + "</span>页</li>");
				}
				if (refresh) {
					builder
							.append("<li class='refresh' title='刷新' onclick='javascript:refreshPage();'><span>刷新</span></li>");
				}
				builder.append("</ul></div>");
				super.pageContext.getOut().print(builder.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}